package com.zzuli.dcxt.controller;

import com.zzuli.dcxt.entiy.Dishes;
import com.zzuli.dcxt.service.DishesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/dishes")
public class DisheseController {
    @Autowired
    private DishesService dishesService;

    //查询所有的菜单
    @RequestMapping("/select")
    public String selectAll(HttpSession session){

        List<Dishes> dishes = dishesService.selectAll();
        session.setAttribute("dishes",dishes);
        return "booklist";
    }

    @RequestMapping("/add")
    public String addDisehs(Dishes dishes, HttpServletRequest  request){
        System.out.println(dishes);
        dishesService.addDishes(dishes);
        List<Dishes> dishesList = dishesService.selectAll();
        request.setAttribute("dishes",dishesList);
//        return "booklist";
        return "redirect:/dishes/select.do";
    }

    @RequestMapping("/selectOne")
    public String selectOne(int id, HttpSession session){
     Dishes dishes= dishesService.selectOne(id);
     session.setAttribute("updateDishe",dishes);
     return "update";
    }

    @RequestMapping("/update")
    public String update(Dishes dishes){
        System.out.println(dishes);
        dishesService.update(dishes);
        return "redirect:/dishes/select.do";
    }

    @RequestMapping("/delete")
    public String delete(int id){
        dishesService.delete(id);
        return "redirect:/dishes/select.do";
    }
}
